package com.homegoods.videomodule.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.homegoods.videomodule.R;
import com.kedll.library.listener.SimpleAnimationListener;
import com.kedll.library.util.BindingUtil;

import cn.jzvd.JZDataSource;
import cn.jzvd.JzvdStd;

/**
 * @author QiQi
 * @ClassName: JzvdShortVideo
 * @date 2018/10/8
 * @description
 */
public class ShortVideo extends JzvdStd implements ISetShortVideoInfo {

    private ImageView mHead, ivLike;
    private TextView mTitle, mIntro, mLikeCount, mCommentCount, mPosition, mAttention;
    private ViewGroup mViewContain;
    private OnShortVideoListener onShortVideoListener;
    private boolean hasLike;

    /**
     * 是否已关注，默认未关注
     */
    private boolean isAttention = false;

    public ShortVideo(Context context) {
        super(context);
    }

    public ShortVideo(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnShortVideoListener(OnShortVideoListener onShortVideoListener) {
        this.onShortVideoListener = onShortVideoListener;
    }


    @Override
    public void init(Context context) {
        super.init(context);

        thumbImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ViewCompat.setTransitionName(thumbImageView, "image");

        textureViewContainer.setOnClickListener(null);
        textureViewContainer.setOnTouchListener(null);
        View mLike = findViewById(R.id.ll_like);
        View mllComment = findViewById(R.id.ll_comment);
        View mForward = findViewById(R.id.ll_forward);
        View mRlComment = findViewById(R.id.rl_comment);
        mHead = findViewById(R.id.iv_head);
        mTitle = findViewById(R.id.tv_title);
        mIntro = findViewById(R.id.tv_intro);
        mLikeCount = findViewById(R.id.tv_like_count);
        mCommentCount = findViewById(R.id.tv_msg_count);
        mPosition = findViewById(R.id.tv_position);
        mAttention = findViewById(R.id.tv_attention);
        ivLike = findViewById(R.id.iv_like);
        mViewContain = findViewById(R.id.view_contain);

        mLike.setOnClickListener(this);
        mllComment.setOnClickListener(this);
        mForward.setOnClickListener(this);
        mRlComment.setOnClickListener(this);
        mAttention.setOnClickListener(this);
    }

    @Override
    public void setUp(JZDataSource jzDataSource, int screen) {
        jzDataSource.looping = true;
        super.setUp(jzDataSource, screen);
    }

    @Override
    public int getLayoutId() {
        return R.layout.video_layout_short_video;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (onShortVideoListener == null) {
            return;
        }

        int id = v.getId();
        if (id == R.id.ll_forward) {
            onShortVideoListener.onForwardClick();
        } else if (id == R.id.ll_comment || id == R.id.rl_comment) {
            onShortVideoListener.onCommentClick();
        } else if (id == R.id.ll_like) {
            createLikeAnim();
            onShortVideoListener.onLikeClick();
        } else if (id == R.id.tv_attention) {
            onShortVideoListener.onAttentionClick();
        }

    }

    @Override
    public void setAllControlsVisiblity(int topCon, int bottomCon, int startBtn, int loadingPro, int thumbImg, int bottomPro, int retryLayout) {
        super.setAllControlsVisiblity(INVISIBLE, INVISIBLE, INVISIBLE, INVISIBLE, thumbImg, INVISIBLE, retryLayout);
    }

    private void createLikeAnim() {
        if (!hasLike) {
            //把红心添加到动画层
            int[] loc = new int[2];
            //获取白心在屏幕中的位置
            ivLike.getLocationInWindow(loc);
            ivLike.setImageResource(R.drawable.video_like_red);
            mLikeCount.setTextColor(Color.RED);
            final ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(R.drawable.video_like_red);
            //设置红心的在屏幕中的位置
            ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.leftMargin = loc[0];
            lp.topMargin = loc[1];
            lp.topToTop = 0;
            lp.leftToLeft = 0;
            //在动画层中添加红心
            mViewContain.addView(imageView, lp);

            //执行动画
            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.video_like_anim);
            animation.setAnimationListener(new SimpleAnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    super.onAnimationEnd(animation);
                    //动画完成后，把红心从动画层中移除
                    mViewContain.post(() -> mViewContain.removeView(imageView));
                }
            });
            imageView.startAnimation(animation);

        } else {
            ivLike.setImageResource(R.drawable.video_like_white);
            mLikeCount.setTextColor(Color.WHITE);
        }
        hasLike = !hasLike;
    }

    @Override
    public void setHeadUrl(String headUrl) {
        BindingUtil.loadCircleImage(mHead, headUrl, null);
    }

    @Override
    public void setTitle(String title) {
        mTitle.setText(title);
    }

    @Override
    public void setSubTitle(String subTitle) {
        mIntro.setText(subTitle);
    }

    @Override
    public void setPosition(String position) {
        mPosition.setText(position);
    }

    @Override
    public void setLikeCount(String likeCount) {
        mLikeCount.setText(likeCount);
    }


    @Override
    public void setCommentCount(String commentCount) {
        mCommentCount.setText(commentCount);
    }

    @Override
    public void setLikeView(boolean isLike) {
        hasLike = isLike;
        createLikeAnim();
    }

    @Override
    public void setAttentionSuc() {
        isAttention = !isAttention;
        if (isAttention) {
            mAttention.setText("已关注");
            mAttention.setBackgroundResource(R.drawable.un_gz);
        } else {
            mAttention.setText("+关注");
            Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.tab_shape);
            mAttention.setBackground(drawable);
        }
    }


    /**
     * 直接开始播放
     */
    public void directPlay() {
        startButton.performClick();
    }
}
